<template>
	<view class="basket">
		<view class="top">
			<view class="goBackButton" @click="goBack()">
				<image style="width: 100%;height: 100%;" src="../../static/icons/Iconly_Light_Arrow - Left@1x.png"
					mode="center">
				</image>
			</view>

			<view class="middle">
				Basket
			</view>

			<view class="deleteButton">
				<image style="width: 100%;height: 100%;" src="../../static/icons/Iconly_Light_Delete@1x.png"
					mode="center"></image>
			</view>
		</view>


		<scroll-view class="goodList" scroll-y="true" :show-scrollbar="false">
			<view v-for="(item,index) in basketData" class="goodItem">
				<view class="cover">
					<image style="width: 100%;" :src="item.cover" mode="widthFix"></image>
				</view>

				<view class="info">
					<view class="goodName">
						{{item.goodName}}
					</view>

					<view class="price">
						$ {{item.price}}
					</view>

					<view class="counter">
						<view class="text">Quantity</view>
						<view class="button" @click="reduceQuality(index)">
							-
						</view>
						<view class="num">
							{{item.quality}}
						</view>
						<view class="button" @click="addQuality(index)">
							+
						</view>
					</view>
				</view>
			</view>
		</scroll-view>



		<view class="total">
			<view class="text">
				Total
			</view>

			<view class="num">
				$ {{totalPrice}}
			</view>
		</view>


		<view class="checkoutButton" @click="goCheckoutPage">
			<view class="text">
				Checkout
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		computed,
		ref
	} from 'vue';
	// 返回上一页
	const goBack = () => {
		uni.navigateBack()
	}

	// 购物车列表
	const basketData = ref([{
			cover: "../../static/images/search/ipadair.png",
			goodName: "2020 Apple iPad Air 10.9",
			price: 567.00,
			quality: 1
		},
		{
			cover: "../../static/images/basket/air pod.png",
			goodName: "APPLE AirPods Pro - White",
			price: 375.00,
			quality: 1
		}
	])

	const reduceQuality = (index) => {

		basketData.value[index].quality !== 1 && basketData.value[index].quality--
	}

	const addQuality = (index) => {
		basketData.value[index].quality++
	}

	const totalPrice = computed(() => {
		let total = 0;
		basketData.value.forEach((item) => {
			total += (item.price * item.quality)
		})
		return total
	})


	const goCheckoutPage = () => {
		uni.navigateTo({
			url: `/pages/checkout/checkout?totalPrice=${totalPrice.value}&productNum=${basketData.value.length}`
		})
	}
</script>

<style lang="scss" scoped>
	.basket {
		width: 100vw;
		min-height: 100vh;
		height: 100%;
		background-color: rgba(246, 246, 249, 1);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: start;

		.top {
			margin-top: 120rpx;
			width: 100%;
			padding: 0 58rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.middle {
				font-size: 36rpx;
				font-weight: 700;
				letter-spacing: 0px;
				line-height: 0px;
				color: rgba(0, 0, 0, 1);
			}

			.goBackButton,
			.deleteButton {
				width: 48rpx;
				height: 48rpx;
			}
		}

		.goodList {
			height: 65vh;
			width: 100%;
			// background-color: black;
			padding: 0 100rpx;
			margin-top: 100rpx;
			// overflow-y: scroll;

			.goodItem {
				margin-top: 32rpx;
				width: 100%;
				height: 260rpx;
				border-radius: 20rpx;
				background: rgba(255, 255, 255, 1);
				box-shadow: 0px 10px 40px rgba(0, 0, 0, 0.03);
				padding: 28rpx 30rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				overflow: hidden;
				gap: 18rpx;


				.cover {
					width: 160rpx;
					overflow-x: hidden;
				}

				.info {
					height: 100%;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					align-items: start;

					.goodName {
						text-overflow: ellipsis;
						font-size: 32rpx;
						font-weight: 600;
						letter-spacing: 0px;
						line-height: 18.78px;
						color: rgba(0, 0, 0, 1);
						// white-space: nowrap;
					}

					.price {
						font-size: 30rpx;
						font-weight: 600;
						letter-spacing: 0px;
						line-height: 17.61px;
						color: rgba(89, 86, 233, 1);
					}

					.counter {
						display: flex;
						align-items: center;
						justify-content: center;
						gap: 24rpx;

						.text {
							font-size: 26rpx;
							font-weight: 400;
							letter-spacing: 0.52px;
							line-height: 15.23px;
							color: rgba(0, 0, 0, 1);
						}

						.button {
							width: 40rpx;
							height: 40rpx;
							background-color: rgba(125, 204, 236, 1);
							border-radius: 8rpx;
							line-height: 40rpx;
							text-align: center;
							color: white;

							&:active {
								opacity: 0.8;
							}
						}


						.num {
							font-size: 26rpx;
							font-weight: 600;
							letter-spacing: 0px;
							line-height: 15.23px;
							color: rgba(0, 0, 0, 1);
						}
					}
				}

				&:nth-child(1) {
					margin-top: 0;
				}



			}
		}


		.total {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			padding: 0 70rpx;
			margin-top: 60rpx;

			.text {
				font-size: 34rpx;
				font-weight: 400;
				letter-spacing: 0px;
				line-height: 19.92px;
			}

			.num {
				font-size: 44rpx;
				font-weight: 700;
				letter-spacing: 0px;
				line-height: 25.83px;
				color: rgba(89, 86, 233, 1);
			}
		}



		.checkoutButton {
			width: 628rpx;
			height: 140rpx;
			border-radius: 20rpx;
			background: rgba(89, 86, 233, 1);
			box-shadow: 0px 20px 40px rgba(0, 0, 0, 0.1);
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 20rpx;
			margin-top: 8rpx;

			&:active {
				opacity: 0.8;
			}

			.text {
				font-size: 40rpx;
				font-weight: 700;
				letter-spacing: 0px;
				line-height: 23.48px;
				color: rgba(246, 246, 249, 1);
			}
		}
	}
</style>